import * as CryptoJS from 'crypto-js';

class EncryptionService {
    private readonly secretKey: string;
    private readonly iv: string;

    constructor(secretKey: string, iv: string) {
        this.secretKey = secretKey;
        this.iv = iv;
    }

    // AES encryption
    encrypt(text: string): string {
        const key = CryptoJS.enc.Utf8.parse(this.secretKey);
        const iv = CryptoJS.enc.Utf8.parse(this.iv);
        const encrypted = CryptoJS.AES.encrypt(text, key, {iv: iv});
        return encrypted.toString();
    }

    // AES decryption
    decrypt(cipherText: string): string {
        const key = CryptoJS.enc.Utf8.parse(this.secretKey);
        const iv = CryptoJS.enc.Utf8.parse(this.iv);
        const decrypted = CryptoJS.AES.decrypt(cipherText, key, {iv: iv});
        return decrypted.toString(CryptoJS.enc.Utf8);
    }
}

const secretKey = 'ST&f7<yw$5x6RSQww$bGk+CQblc6LC3c'; // 密钥
const iv = '0123456789abcdef'; // 初始化向量

const encryptionService = new EncryptionService(secretKey, iv);

export default encryptionService;
